from app.models import Checkout, ValueSetInterface, DataSet
from google.appengine.ext import db
from gdata.spreadsheet.service import SpreadsheetsService
import gdata
import StringIO
import re
import csv
import time, datetime
import httplib

MAX_RESULTS = 100

NAME_MATCHER = re.compile(r'^n([0-9]+)\.')

def download_spreadsheet(job):
  client = get_client(job)
  dataset = DataSet.get(job.dataset_key)
  fields_list = job.fields_list
  user = job.user
  spreadsheet_key = job.spreadsheet_key
  
  interface = ValueSetInterface(user, dataset)
  
  start = 1
  while True:
    row_query = gdata.spreadsheet.service.ListQuery()
    row_query.start_index = start
    row_query.max_results = MAX_RESULTS
    
    feed = client.GetListFeed(spreadsheet_key, query=row_query)
    for entry in feed.entry:
      entries = [None for i in fields_list]
      for key,value in entry.custom.iteritems():
        match = NAME_MATCHER.match(key.strip())
        if not match: continue
        index = int(match.group(1)) - 1
        entries[index] = value.text
      
      current_fieldset = None
      current_dict = None
      args = []
      for entry, field in zip(entries, fields_list):
        if not entry: continue
        fieldset,name = field.split('#')
        if fieldset != current_fieldset:
          if current_fieldset:
            args.append((current_fieldset, current_dict))
          current_dict = {}
          current_fieldset = fieldset
        current_dict[name] = entry
      
      if current_fieldset:
        args.append((current_fieldset, current_dict))
      
      if not args: continue
      
      interface.insert_or_update_row(*args)
    
    interface.commit()
    if len(feed.entry) < MAX_RESULTS:
      checkout = Checkout.get(job.checkout_key)
      checkout.checked_in_on = datetime.datetime.now()
      checkout.checked_in = True
      checkout.put()
      return "$APP_ENGINE_URL/checkout/list"
    else:
      start += MAX_RESULTS
  
def get_client(job):
  """ Returns a DocsService client, authenticates """
  client = SpreadsheetsService()
  client.ClientLogin(job.gdocs_user, job.gdocs_password)
  return client